package com.eqdd.yiqidian.ui.tongxunlu.tag;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.InputType;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.yiqidian.BiaoqianActivityCustom;
import com.eqdd.yiqidian.ListItem7Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.TagListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class TagActivity extends BaseActivity {
    private BiaoqianActivityCustom dataBinding;
    private String[] items;
    private ArrayList<String> strings;
    private SlimAdapterEx<TagListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_biaoqian);
        initTopTitleBar(View.VISIBLE, "标签");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_TAG)
                    .params("owner", user.getGuid())
                    .params("labelid", slimAdapterEx.getDataItem(pos).getId())
                    .execute(new DialogCallBack<HttpResult>(TagActivity.this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                menuBridge.closeMenu();
                                slimAdapterEx.getData().remove(pos);
                                slimAdapterEx.notifyItemRemoved(pos);
                            }
                        }
                    });
        });
        ListItem7Custom headCos = DataBindingUtil.inflate(getLayoutInflater(), R.layout.list_item_07, null, false);
        headCos.tvContent.setText("+新建标签");
        headCos.tvContent.setTextColor(getResources().getColor(R.color.green));
        dataBinding.recyclerView.addHeaderView(headCos.getRoot());
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_07, new SlimInjector<TagListResultBean>() {
                    @Override
                    public void onInject(TagListResultBean data, IViewInjector injector) {

                        injector.text(R.id.tv_content, data.getName());
                        injector.textColor(R.id.tv_content, getResources().getColor(R.color.black));
                    }
                }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", slimAdapterEx.getDataItem(position).getName());
            bundle.putString(Config.LABLEID, slimAdapterEx.getDataItem(position).getId() + "");
            startActivity(TagItemActivity.class, bundle);
        });
        dataBinding.recyclerView.setSwipeItemLongClickListener((view, position) -> {

            new MaterialDialog.Builder(TagActivity.this)
                    .title("更改标签名")
                    .inputType(InputType.TYPE_CLASS_TEXT)//可以输入的类型-电话号码
                    //前2个一个是hint一个是预输入的文字
                    .input("标签名", slimAdapterEx.getDataItem(position).getName(), (dialog, input) -> {

                    })
                    .onPositive((dialog, which) -> {
//                            if (dialog.getInputEditText().length() <=10) {
//
//                                dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
//                            }else {
//                                dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
//                            }
                    })
                    .show();
        });
        ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
            if (position == 0) {
                startActivityForResult(AddTagActivity.class, null, Config.ADD_TAG);
            }
        });
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<List<TagListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.TAG_LIST)
                .params("owner", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<TagListResultBean>>>(TagActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<TagListResultBean>>> response) {
                        HttpResult<List<TagListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.ADD_TAG) {
                setView();
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
